// CameraAnalyzer.kt
package com.example.aieyes

import android.content.Context
import android.graphics.ImageFormat
import android.util.Log
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy
import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer

class CameraAnalyzer(
    private val context: Context,
    private val onImageCaptured: (ByteArray) -> Unit
) : ImageAnalysis.Analyzer {

    override fun analyze(image: ImageProxy) {
        val mediaImage = image.image ?: return

        // 转为 JPEG
        val buffer = mediaImage.planes[0].buffer
        val bytes = ByteArray(buffer.remaining())
        buffer.get(bytes)

        // 如果是 YUV 格式，需转换（简化：假设 JPEG）
        // 实际项目建议用 ImageUtil（Google 示例）
        onImageCaptured(bytes)

        image.close()
    }
}